package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.control.labels.LabelData;
import com.wgts.wgts.localbean.LocalMoreProductInfo;
import com.wgts.wgts.localbean.LocalProductInfo;
import com.wgts.wgts.localbean.ProductImages;
import com.wgts.wgts.localbean.ProductInfoAttribute;
import com.wgts.wgts.localbean.ProductSpecifications;
import com.wgts.wgts.mvp.model.IJDGoodInfoActivityModel;
import com.wgts.wgts.mvp.presenter.IJDGoodInfoActivityPersenter;
import com.wgts.wgts.mvp.view.IJDGoodinfoActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.AddShopCarRequest;
import com.wgts.wgts.netmodle.req.AffirmOrderProduct;
import com.wgts.wgts.netmodle.req.CollectionProductRequest;
import com.wgts.wgts.netmodle.req.MoreMoreGoodsInfoReq;
import com.wgts.wgts.netmodle.req.ProductInfoRequest;
import com.wgts.wgts.netmodle.req.UpDownProductRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.MoreMoreGoodsInfoEntity;
import com.wgts.wgts.netmodle.resp.ProductInfoEntity;

import java.math.BigDecimal;
import java.util.ArrayList;

public class JDGoodInfoActivityPersenter implements IJDGoodInfoActivityPersenter {

    private IJDGoodInfoActivityModel model;

    private IJDGoodinfoActivityView view;

    private ArrayList<ProductInfoAttribute> attributes;

    private ArrayList<ProductSpecifications> specifications;

    private String[] selectSpecifications;

    private LocalProductInfo productInfo;

    private ProductInfoAttribute selectAttributes;



    private int mount = 1;

    public JDGoodInfoActivityPersenter(IJDGoodInfoActivityModel model, IJDGoodinfoActivityView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void nomalProductInfo(final String productId) {
        ProductInfoRequest request = new ProductInfoRequest();
        request.productId = productId;
        model.productInfo(request, new HttpResultSubscriber<ProductInfoEntity>() {
            @Override
            public void onSuccess(ProductInfoEntity productInfoEntity) {
                product(productInfoEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    private void product(ProductInfoEntity productInfoEntity){
        productInfo = new LocalProductInfo();
        productInfo.marketPrice = productInfoEntity.marketPrice;
        productInfo.productId = productInfoEntity.productId;
        productInfo.productName = productInfoEntity.productName;
        productInfo.productSubtitle = productInfoEntity.productSubtitle;
        productInfo.updown = productInfoEntity.updown;
        productInfo.collected = productInfoEntity.collected;
        productInfo.retailPrice = productInfoEntity.retailPrice;
        productInfo.sellCount = productInfoEntity.sellCount;

        productInfo.productUrl = productInfoEntity.productUrl;

        productInfo.productSmallImage = productInfoEntity.productSmallImage;

        productInfo.restTime = productInfoEntity.restTime;

        productInfo.activitySkuId = productInfoEntity.activitySkuId;

        productInfo.status = productInfoEntity.status;

        productInfo.productStandard = productInfoEntity.productStandard;


        ArrayList<ProductImages> productBanner = new ArrayList<>();

        for(ProductInfoEntity.ImageType item : productInfoEntity.productImages){
            ProductImages banner = new ProductImages();
            banner.filePath = item.filePath;
            banner.height = item.height;
            banner.width = item.width;
            productBanner.add(banner);
        }

        ArrayList<ProductImages> productBig = new ArrayList<>();

        for(ProductInfoEntity.ImageType item : productInfoEntity.productBigImage){
            ProductImages bigImg = new ProductImages();
            bigImg.filePath = item.filePath;
            bigImg.height = item.height;
            bigImg.width = item.width;
            productBig.add(bigImg);
        }

        attributes = new ArrayList<>();
//        for(ProductInfoEntity.ProductAttribute item : productInfoEntity.skuList){
//            ProductInfoAttribute attribute = new ProductInfoAttribute();
//            attribute.amount = item.amount;
//            attribute.attribute = item.attribute;
//            attribute.id = item.id;
//            attribute.image = item.image;
//            attribute.key = item.key;
//            attribute.productId = item.productId;
//            attribute.retailPrice = item.retailPrice;
//            attribute.activitySkuId = item.activitySkuId;
//            attributes.add(attribute);
//        }

        specifications = new ArrayList<>();
//        for(ProductInfoEntity.Specifications item : productInfoEntity.standardList){
//            ProductSpecifications specifi = new ProductSpecifications();
//            specifi.attributeId = item.attributeId;
//            specifi.attributeName = item.attributeName;
//            specifi.attributeValueList = item.attributeValueList;
//            specifications.add(specifi);
//        }

//        selectSpecifications = new String[specifications.size()];
//        for(int i = 0; i < specifications.size(); i++){
//            selectSpecifications[i] = "|" + specifications.get(i).attributeId + "|" + specifications.get(i).attributeValueList.get(0);
//        }

        view.productResult(productBanner, productBig, productInfo, attributes, specifications);
//        view.selectResult(attributes.get(0));
//        selectAttributes = attributes.get(0);


        view.initCollectionResult(productInfo.collected);
    }




    @Override
    public void selectLabs(LabelData data, int position, boolean isSelect) {

        StringBuilder searchKey = new StringBuilder();
        searchKey.append(productInfo.productId);
        if(isSelect){
            selectSpecifications[data.getId()] =  "|" + specifications.get(position).attributeId + "|" + data.getName();
        }else{
            selectSpecifications[data.getId()] =  "";
        }
        for(String item : selectSpecifications){
            if(item != null && !item.equals("")){
                searchKey.append(item);
            }
        }
        for(ProductInfoAttribute item : attributes){
            if(item.key.equals(searchKey.toString())){
                selectAttributes = item;
                view.selectResult(item);
            }
        }


    }

    @Override
    public void addmount() {
        if(selectAttributes.amount < mount){
            view.mountOver();
        }else {
            mount++;
            Double price = Double.valueOf(selectAttributes.retailPrice);
            BigDecimal mountPrice = BigDecimal.valueOf(price).multiply(BigDecimal.valueOf(mount));
            view.mountPrice(String.valueOf(price));
            view.mount(mount);
        }
    }

    @Override
    public void productmount() {
        if(mount <= 1){
            view.roduceError();
        }else {
            mount--;
            Double price = Double.valueOf(selectAttributes.retailPrice);
            view.mountPrice(String.valueOf(price));
            view.mount(mount);
        }
    }

    @Override
    public void collectionProduct(int collection) {
        if(productInfo != null){
            CollectionProductRequest request = new CollectionProductRequest();
            request.collected = collection;
            request.productId = productInfo.productId;
            model.collectionProduct(request, new HttpResultSubscriber<Integer>() {
                @Override
                public void onSuccess(Integer currentEntity) {
                    view.collectionProductResult(currentEntity);
                }

                @Override
                public void _onError(int status, String msg) {
                    view.onError(status, msg);
                }
            });
        }
    }

    @Override
    public void addShopCar() {
        if(this.mount <= 0){
            view.onError(0, "商品数量不能小于1");
            return;
        }
        AddShopCarRequest request = new AddShopCarRequest();
        request.amount = this.mount;
        request.productId = productInfo.productId;
        request.skuId = selectAttributes.id;
        model.addShopCar(request, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.addShopResult();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void moreMoreActionInfo(int drawId) {
        MoreMoreGoodsInfoReq req = new MoreMoreGoodsInfoReq();
        req.drawId = drawId;
        model.moreMore(req, new HttpResultSubscriber<MoreMoreGoodsInfoEntity>() {
            @Override
            public void onSuccess(MoreMoreGoodsInfoEntity entity) {
                LocalMoreProductInfo localMoreProductInfo = new LocalMoreProductInfo();
                localMoreProductInfo.productId = entity.productId;
                localMoreProductInfo.productName = entity.productName;
                localMoreProductInfo.productStandard = entity.productStandard;
                localMoreProductInfo.restTime = entity.restTime;
                localMoreProductInfo.sharePath = entity.shareCount;
                localMoreProductInfo.shareCount = entity.shareCount;

                ArrayList<ProductImages> productBanner = new ArrayList<>();

                for(MoreMoreGoodsInfoEntity.ProductImage item : entity.productImages){
                    ProductImages banner = new ProductImages();
                    banner.filePath = item.filePath;
                    productBanner.add(banner);
                }

                ArrayList<ProductImages> productBig = new ArrayList<>();

                for(MoreMoreGoodsInfoEntity.ProductBigImage item : entity.productBigImage){
                    ProductImages bigImg = new ProductImages();
                    bigImg.filePath = item.filePath;
                    productBig.add(bigImg);
                }

            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void affirmOrder() {

        if(this.mount <= 0){
            view.onError(0, "商品数量不能小于1");
            return;
        }

        AffirmOrderProduct affirmOrderProduct = new AffirmOrderProduct();
        //affirmOrderProduct.activitySkuId = String.valueOf(selectAttributes.activitySkuId);
        affirmOrderProduct.productId = String.valueOf(productInfo.productId);
        affirmOrderProduct.productNum = String.valueOf(mount);
        //affirmOrderProduct.skuId = String.valueOf(selectAttributes.id);
        view.affirmOrder(affirmOrderProduct);
    }

    @Override
    public void upDownProduct(String id, int upDownState) {
        UpDownProductRequest request = new UpDownProductRequest();
        request.productId = Integer.valueOf(id);
        request.updown = upDownState;
        model.updownProduct(request, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                view.upDownProductResult(integer);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void activityProductInfo(Integer productId, Integer activityId, String beginTime, String  endTime) {

        model.activityProductInfo(productId, activityId, beginTime, endTime, new HttpResultSubscriber<ProductInfoEntity>() {
            @Override
            public void onSuccess(ProductInfoEntity productInfoEntity) {
                product(productInfoEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


    @Override
    public void refreshProduct(Integer productId, Integer activityId, String beginTime, String  endTime) {
        model.activityProductInfo(productId, activityId, beginTime, endTime, new HttpResultSubscriber<ProductInfoEntity>() {
            @Override
            public void onSuccess(ProductInfoEntity productInfoEntity) {
                view.refreshProduct(productInfoEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

}
